import 'dart:async';

import 'package:desktop_window/desktop_window.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_getx/app/base/load_image.dart';
import 'package:flutter_getx/core/utils/device_utils.dart';
import 'package:flutter_getx/ext/ext.dart';
import 'package:get/get.dart';
import 'package:rxdart/rxdart.dart';

import 'main/main_routers.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({Key? key}) : super(key: key);

  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {

  StreamSubscription? _subscription;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((_) async {
      if (kIsWeb) {
        _goHome();
      } else {
        precacheImage(AssetImage("splash_top".png), context);
        precacheImage(AssetImage("splash_logo".png), context);
        _initSplash();
      }
    });

    /// 设置桌面端窗口大小
    if (Device.isDesktop) {
      DesktopWindow.setWindowSize(const Size(400, 800));
    }
  }

  @override
  void dispose() {
    _subscription?.cancel();
    super.dispose();
  }

  void _initSplash() {
    _subscription =
        Stream.value(1).delay(const Duration(milliseconds: 1500)).listen((_) {
      _goHome();
    });
  }

  void _goHome() {
    Get.offNamed(homePage);
  }

  @override
  Widget build(BuildContext context) {
    return Material(
        color: Colors.white,
        child: Column(
          children: [
            LoadImage(
              'splash_top'.png,
              fit: BoxFit.fitWidth,
            ),
            Expanded(
                child: Center(
              child: LoadImage(
                'splash_logo'.png,
                width: 150,
                height: 25,
              ),
            ))
          ],
        ));
  }
}
